// pages/assistanceList/assistanceList.js

const http = require('../../utils/http.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //demo: [{ a: '10800' }, { a: '7200000'}],
    assisList:[],
    nid:1
  },
//切换导航栏
  checkNav(e){
    var nid=e.currentTarget.dataset.nid;
    this.setData({
      nid
    })
    this.getAssisList(nid)
  },

  //获取助力列表
  getAssisList(type){
    clearInterval(this.data.timer)
    this.setData({
      assisList:[],
      timer:null
    })
    var uid = app.globalData.uid;
    http.fetchApi('procedure_t', { p: `R2013007|${uid}|${type}` }).then(res=>{
      if(res.data.code==1){
        var assisList = res.data.results;
        this.setData({
          assisList
        })

        // console.log(dates.datetime)//数据
        var assisList = this.data.assisList;
        let that = this;
        let len = assisList.length;//时间数据长度

        function nowTime() {//时间函数
          // console.log(a)
          for (var i = 0; i < len; i++) {
            var intDiff = assisList[i].jssj_datas;//获取数据中的时间戳  秒数
           // console.log(intDiff)
            var day = 0, hour = 0, minute = 0, second = 0;
            if (intDiff > 0) {//转换时间
              day = Math.floor(intDiff / (60 * 60 * 24));
              hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
              minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
              second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
              if (hour <= 9) hour = '0' + hour;
              if (minute <= 9) minute = '0' + minute;
              if (second <= 9) second = '0' + second;
              assisList[i].jssj_datas--;
              var str = `${hour}时${minute}分${second}秒`
              // console.log(str)  
            } else {
              var str = "已结束！";
              //clearInterval(timer);
            }
            // console.log(str);
            assisList[i].difftime = str;//在数据中添加difftime参数名，把时间放进去
          }
          that.setData({
            assisList
          })
          // console.log(that)
        }

        nowTime();
        var timer = setInterval(nowTime, 1000);
        this.setData({
          timer
        })



      }
    })
  },

  //邀请好友  助力详情
  inviteFriends(e){
    var zl_id = e.currentTarget.dataset.zlid;
    
    wx.navigateTo({
      url: '../ShareInvitation/ShareInvitation?zl_id=' + zl_id
    })
   
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAssisList(1);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // clearInterval(this.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})